/*
 * CProcDatabase.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of JRogueDPS.
 * 
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */

package geardatabase;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author Giles Millward (Manigra)
 */
public class CProcDatabase {

    private HashMap<Integer, CProcItem> m_itemMap = new HashMap<Integer, CProcItem>();

    public void initProcItems()
    {
        this.readDatabase();
    }

    public CProcItem getProcItem(int p_Id)
    {
        return m_itemMap.get(p_Id);
    }

    private void addProcItem(CProcItem p_item)
    {
        m_itemMap.put(p_item.m_id, p_item);
    }

    public void readDatabase()
    {
        File l_xml = new File("data/database/procs.xml");
        SAXReader l_reader = new SAXReader();
        Document l_doc;
        try {
            l_doc = l_reader.read(l_xml);
            List l_list = l_doc.selectNodes( "//root/procitem" );

            for (Iterator l_iter = l_list.iterator(); l_iter.hasNext(); ) {
                Element l_element = (Element) l_iter.next();
                this.addProcItem(new CProcItem(l_element));
         }
        } catch (DocumentException ex) {
            Logger.getLogger(CProcDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void writeDatabase()
    {
        Document doc = DocumentFactory.getInstance().createDocument();
        Element root = doc.addElement("root");

        Iterator iterator = m_itemMap.keySet().iterator();
        while (iterator.hasNext()) {
            int l_key = (Integer) iterator.next();
            CProcItem l_item = m_itemMap.get(l_key);
            l_item.addXMLData(root);
        }

        FileOutputStream fos;
        try {
            fos = new FileOutputStream("data/database/procs.xml");
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer;
            try {
                writer = new XMLWriter(fos, format);
                try {
                    writer.write(doc);
                    writer.flush();
                } catch (IOException ex) {
                    Logger.getLogger(CProcDatabase.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CProcDatabase.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(CProcDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
